-- Pixy
-- Created by chengb Feb/3/2016
-- 小妖精格子

-- 初始化
function initPixy(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local dbase = DungeonM.query(class, "dbase");
    if dbase.resued == TRUE then
        -- 这是营救之后的小妖精
        -- 判断玩家是否营救过
        if not NewbieDungeonM.hasResuedPixy() then
            -- 没解救过，格子不显示
            return;
        end
    end

    local pos = self.gridData:getPos();

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("ui_open1");

            if NewbieDungeonM.hasResuedPixy() then
                -- 解救过
                local function okFunc()
                    -- 执行奖励
                    DungeonActionM.go("do_building_bonus", pos);

                    -- 播放妖精飞走的动画
                    local dustEffectParent = findChildByName(self.node, "effect_node");
                    dustEffectParent:setVisible(true);
                    playPixyFleeEffect(self, dustEffectParent);
                end

                self:openBuildingView(okFunc);
            else
                -- 没有解救过
                local function okFunc()
                    -- 索要物品
                    local tipList;
                    local grid_bonus = self.gridData.bonus;
                    if type(grid_bonus) == 'table' and #grid_bonus > 0 then
                        -- 无尽模式中出现
                        tipList = splitString(getLocStr("pixy_give_item3"), "\n");
                    elseif NewbieDungeonM.isFirstNewbieBonus("pixy") then
                        -- 首次获得
                        tipList = splitString(getLocStr("pixy_give_item"), "\n");
                    else
                        tipList = splitString(getLocStr("pixy_give_item2"), "\n");
                    end

                    splitAlert(tipList);

                    -- 执行奖励
                    DungeonActionM.go("do_building_bonus", pos);

                    -- 索要物品
                    DungeonActionM.go("ask_for_items", pos);

                    -- 播放妖精飞走的动画
                    local dustEffectParent = findChildByName(self.node, "effect_node");
                    dustEffectParent:setVisible(true);
                    playPixyFleeEffect(self, dustEffectParent);
                end

                local function cancelFunc()
                    -- 让它离开
                    DungeonActionM.go("rescue_pixy", pos);
                end

                self:openBuildingView2(okFunc, cancelFunc);
            end
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 透明处理
    self.icon:setOpacity(0);

    local dustEffectParent = findChildByName(self.node, "effect_node");
    dustEffectParent:setVisible(true);

    if not self.hasPixyEffect then
        -- 没有小精灵动画，构建一个
        playPixyEffect(self, dustEffectParent);
    end
end

-- 妖精被放走的事件回调
function onRescuePixy(self)
    alert(getLocStr("pixy_come_back_tip"));

    local dustEffectParent = findChildByName(self.node, "effect_node");
    dustEffectParent:setVisible(true);

    -- 播放妖精飞走的动画
    playPixyFleeEffect(self, dustEffectParent);

    self.item:removeAllChildren();
end

